import { computed, onMounted } from "vue";
import { useListPage } from "@/composables/useListPage";
import inquiryApi, {
  type InquiryListItem,
  type InquiryListParams,
  type InquiryListResult
} from "@/api/system/verify/inquiry";

interface InquiryFilterForm {
  keyword: string;
  status: string | number;
}

const DEFAULT_FILTER: InquiryFilterForm = {
  keyword: "",
  status: ""
};

const STATUS_OPTIONS = [
  { label: "全部状态", value: "" },
  { label: "进行中", value: 0 },
  { label: "已报价", value: 1 },
  { label: "已拒绝", value: 2 },
  { label: "已取消", value: 3 }
];

const statusTagType = (status: number) => {
  switch (status) {
    case 1:
      return "success";
    case 2:
      return "danger";
    case 3:
      return "info";
    case 0:
    default:
      return "warning";
  }
};

const formatInteger = (value: number | null | undefined) => {
  if (value === null || value === undefined) return "-";
  return Number.isFinite(value) ? value.toLocaleString("zh-CN") : String(value);
};

const formatAmount = (value: number | null | undefined) => {
  if (value === null || value === undefined) return "-";
  if (!Number.isFinite(value)) return String(value);
  return Number(value).toLocaleString("zh-CN", {
    minimumFractionDigits: 2,
    maximumFractionDigits: 2
  });
};

export function useInquiryList() {
  const {
    formState: filterForm,
    pagination,
    dataList,
    loading,
    onSearch,
    resetForm,
    handlePageChange,
    handleSizeChange
  } = useListPage<InquiryFilterForm, InquiryListItem, InquiryListResult>(
    {
      fetch: (
        params: InquiryFilterForm & { page: number; page_size: number }
      ) => inquiryApi.fetchList(params as InquiryListParams)
    },
    DEFAULT_FILTER,
    {},
    {
      paramsAdapter: params => {
        const query: InquiryListParams = {
          page: params.page,
          page_size: params.page_size
        };

        const keyword = params.keyword?.trim();
        if (keyword) {
          query.keyword = keyword;
        }

        if (params.status !== "" && params.status !== undefined) {
          query.status = Number(params.status);
        }

        return query;
      },
      responseAdapter: (response: InquiryListResult) => {
        const paginationMeta = response?.pagination ?? {
          total: 0,
          pageSize: 10,
          currentPage: 1
        };

        return {
          list: response?.list ?? [],
          total: Number(paginationMeta.total ?? 0),
          pageSize: Number(paginationMeta.pageSize ?? 10),
          currentPage: Number(
            paginationMeta.currentPage ?? paginationMeta.page ?? 1
          )
        };
      }
    }
  );

  const tableData = computed(() => dataList.value ?? []);

  const columns: TableColumnList = [
    { label: "询价单号", prop: "inquiry_no", width: 180 },
    {
      label: "商品信息",
      prop: "product",
      minWidth: 220,
      align: "left",
      slot: "product"
    },
    {
      label: "买家需求",
      prop: "buyer",
      minWidth: 240,
      align: "left",
      slot: "buyer"
    },
    {
      label: "卖家反馈",
      prop: "seller",
      minWidth: 240,
      align: "left",
      slot: "seller"
    },
    {
      label: "运营 / 区域",
      prop: "assist",
      minWidth: 180,
      align: "left",
      slot: "assist"
    },
    { label: "状态", prop: "status", width: 120, slot: "status" },
    { label: "更新时间", prop: "update_time", width: 180 }
  ];

  const handleSearch = () => {
    filterForm.page = 1;
    onSearch();
  };

  const handleResetFilters = () => {
    resetForm();
    onSearch();
  };

  onMounted(() => {
    onSearch();
  });

  return {
    filterForm,
    statusOptions: STATUS_OPTIONS,
    pagination,
    tableData,
    columns,
    loading,
    handleSearch,
    handleResetFilters,
    handlePageChange,
    handleSizeChange,
    statusTagType,
    formatInteger,
    formatAmount
  };
}
